<?php

namespace App\Logging\Service;

use Illuminate\Support\Facades\Log;
use Ramsey\Uuid\Uuid;

class LogCmdService
{
    const PATH_MODE_GENERAL = 'general';
    const PATH_MODE_CLASS = 'class';

    /**
     * @var static 单例模式
     */
    protected static $instance;

    /**
     * 单例模式
     */
    public static function getInstance()
    {
        if (static::$instance instanceof static) {
            return static::$instance;
        }
        static::$instance = new static();
        return static::$instance;
    }

    protected $cmdName = ''; // 命令名
    protected $cmdFullName = ''; // 命令全名
    protected $cmdClass = ''; // 命令类

    /**
     * @return mixed
     */
    public function getCmdName()
    {
        return $this->cmdName;
    }

    /**
     * @param mixed $cmdName
     */
    public function setCmdName($cmdName)
    {
        $this->cmdName = $cmdName;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getCmdFullName()
    {
        return $this->cmdFullName;
    }

    /**
     * @param mixed $cmdFullName
     */
    public function setCmdFullName($cmdFullName)
    {
        $this->cmdFullName = $cmdFullName;
        return $this;
    }

    /**
     * @return mixed
     */
    public function getCmdClass()
    {
        return $this->cmdClass;
    }

    /**
     * @param mixed $cmdClass
     */
    public function setCmdClass($cmdClass)
    {
        $this->cmdClass = $cmdClass;
        return $this;
    }

    /**
     * 基于模式生成命令日志子目录
     * @param $mode
     * @return string
     * @author Chris Yu <chrisyu@crabapple.top> 2024/11/8
     */
    public function generateCmdLogSubDir($mode): string
    {
        if ($mode === self::PATH_MODE_CLASS) {
            return $this->generateCmdLogSubDirBaseClass();
        }
        return '';
    }

    /**
     * 生成命令日志子目录基于类名
     * @return string
     * @author Chris Yu <chrisyu@crabapple.top> 2024/11/8
     */
    protected function generateCmdLogSubDirBaseClass()
    {
        if (!$this->getCmdClass()) {
            return '';
        }

        $subDir = str_replace('\\', '/', $this->getCmdClass());
        $subDir = str_replace(['App/', 'Console/', 'Commands/'], ['', '', ''], $subDir);

        return $subDir;
    }
}
